package com.cytanaa.youlike;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.cytangaa.display.CustomView;

public class CategoryList  extends CustomView {
	String[] category;
	SharedPreferences mPrefs;
	String categoryPreference = "Comedy";
	ListView category_list;
	String tempPrefs;
	MyAdapter adapter;
	int index= 0; 
    private List<Map<String, Object>> mData = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {    
    	super.onCreate(savedInstanceState);   

        setContentView(R.layout.country);
		titlebar_title.setText("Category");
		titlebar_icon.setImageResource(R.drawable.titlebar_preference);
		mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
		tempPrefs = mPrefs.getString("category", "Entertainment");
		index = mPrefs.getInt("category_index", 3);
		category_list = (ListView) findViewById(R.id.country_list);

    	category = getResources().getStringArray(R.array.interested);

		//https://gdata.youtube.com/feeds/api/standardfeeds/JP/top_rated_Comedy?v=2&alt=jsonc
	    
    	Log.v("In CountryList onCreate","before size");       

	    mData=getData(mData);
    	adapter = new MyAdapter(this);
    	Log.v("In CountryList onCreate","size"+mData.size());       
    	   	
    	category_list.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        		String category = (String)mData.get(position).get("category");   
        		showInfo (category, position);

	    		
        		}
        	});
    	category_list.setAdapter(adapter);
        
        	
    }
    
    public void showInfo(String cate, int pos){
    	tempPrefs=category[pos];
		final String tempCategory = category[pos];
		final int pos1= pos;
		
        new AlertDialog.Builder(this)
        .setTitle("Changing preference Counrty")
        .setMessage("Change your default country to "+cate+" ?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            	
        		mPrefs = getSharedPreferences("facebook_session",MODE_PRIVATE);
        		//int tempIndex =  mPrefs.getInt("country_index", 37);
        		SharedPreferences.Editor editor = mPrefs.edit();
        		editor.putString("category", tempCategory);
        		editor.putInt("category_index", pos1);
        		editor.commit();
        		index = mPrefs.getInt("category_index", 3);
            	Log.v("In countryIndex After","categoryIndex= "+ index);       
                Toast.makeText(CategoryList.this, "Your country: "+tempPrefs, Toast.LENGTH_LONG).show();
                category_list.setAdapter(adapter);
            	
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            	//dismiss();
            }
        })
        .show();
         
    }
    
    
    
    private List<Map<String, Object>> getData(List<Map<String, Object>> mList) {
    	
    	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    	if(mList!=null){
        	list = mList;
    	}
        Log.v("In getData","length"+category.length);       
        Map<String, Object> map;        

    	for(int j=0; j<category.length; j++){
    		map = new HashMap<String, Object>();
    		map.put("category", category[j]);    
            Log.v("In getData","category = "+category[j]);      
            list.add(map);
    		}

        Log.v("In getData","list: "+list.size());       

        return list;
    }
    
    
    public final class ViewHolder{
    	public CheckBox check;
        public TextView countryName;

    }
    public class MyAdapter extends BaseAdapter{
    	private LayoutInflater mInflater;
        ViewHolder holder = null;
         
        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }
 
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
 
        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /**Original holder
             * 
             * 
             * */
            //ViewHolder holder = null;
            if (convertView == null) {
                 
                holder=new ViewHolder();  
                convertView = mInflater.inflate(R.layout.country_list_row, null);
                holder.countryName=(TextView)convertView.findViewById(R.id.country_text);
                holder.check = (CheckBox)convertView.findViewById(R.id.country_check);
            	Log.v("convertView == null","");  
                convertView.setTag(holder);

                 
            }else {                 
                holder = (ViewHolder)convertView.getTag();
            	Log.v("convertView != null"," != null");       

            }
            
        	Log.v("In CountryList","mData.get("+position+").get(country) = "+(String)mData.get(position).get("country"));       
        	int tag = position;
            holder.countryName.setText((String)mData.get(position).get("category"));
            //holder.check.setOnCheckedChangeListener(new myCheckBoxListener(tag));

            //if (position == countryIndex){
            if (tag == index){
            	Log.v("In CountryList onCreate","before setChecked(true), countryIndex=="+index);       
            	Log.v("In CountryList onCreate","before setChecked(true), position=="+tag);       
            	holder.check.setChecked(true);
            }else{
            	Log.v("In CountryList onCreate","setChecked(false), countryIndex=="+index);       
            	Log.v("In CountryList onCreate","setChecked(false), position=="+position);       
            	holder.check.setChecked(false);
            }
            
              
            return convertView;
        }
        

    	
    }

    
    
}
